package com.lx;

import com.lx.mapper.UserMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 启动类
 * @author 段誉
 * @create 2018-12-17 10:56
 */
public class App {
  public static void main(String[] args) throws IOException {
    /**
     * SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像.
     * SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得,
     * SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例
     * 每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心.
     * SqlSessionFactory也是线程安全的,SqlSessionFactory一旦被创建,应该在应用执行期间都存在.
     * 在应用运行期间不要重复创建多次,建议使用单例模式.SqlSessionFactory是创建SqlSession的工厂
     */
    SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sessionFactory = factoryBuilder.build(resource);
    SqlSession sqlSession = sessionFactory.openSession();
    /* 这里通过jdk的动态代理获取usermapper接口的代理类  */
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<Map> list = userMapper.selectUser();
    System.out.println(list.size());
  }
}
